package Connection;

import java.awt.Point;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.util.ArrayList;

import Model.Player.Player;
import Model.Universe.Universe;
import Model.Visible.Planet;

import com.thoughtworks.xstream.XStream;

public class SendAction {
	
	public XStream xStream;
	public Socket client;
	public String addr_;
	public int port_;
	public ObjectOutputStream writer;
	
	public SendAction(String addr, int port) {
		// TODO Auto-generated constructor stub
		xStream = new XStream();
		client = new Socket();
		addr_ = addr;
		port_ = port;
		writer = null;
	}
	
	
	public void sendMessage(String message){
	try {
		client = new Socket(addr_, port_);
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	try {
		writer = new ObjectOutputStream(client.getOutputStream());
		writer.flush();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	try {
		System.out.println("SENDprexml:"+message);
		writer.writeObject(new String(xStream.toXML(message)));
		writer.flush();
	} catch (IOException e1) {
		// TODO Auto-generated catch block
		e1.printStackTrace();
	}
	try {
		client.getOutputStream().flush();
		client.close();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	try {
		writer.close();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	}
	
	public void sendUniverse(Universe universe){
		try {
			client = new Socket(addr_, port_);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			writer = new ObjectOutputStream(client.getOutputStream());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			System.out.println("Serializing universe");
			String str = new String(xStream.toXML(universe));
			System.out.println("Done serialization");
			System.out.println("Sending universe");
			writer.writeObject(str);
			System.out.println("Sent");
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			client.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			writer.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void sendPlanets(ArrayList<Planet> planets){
		try {
			client = new Socket(addr_, port_);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			writer = new ObjectOutputStream(client.getOutputStream());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			System.out.println("Serializing planets");
			String str = new String(xStream.toXML(planets));
			System.out.println("Done serialization");
			System.out.println("Sending universe");
			writer.writeObject(str);
			System.out.println("Sent");
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			client.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			writer.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void sendPositions(Point[] positions){
		try {
			System.out.println("mandando positions");
			client = new Socket(addr_, port_);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			writer = new ObjectOutputStream(client.getOutputStream());
			writer.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			writer.writeObject(new String(xStream.toXML(positions)));
			writer.flush();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			client.getOutputStream().flush();
			client.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			writer.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void sendPlayer(Player player){
		try {
			client = new Socket(addr_, port_);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			writer = new ObjectOutputStream(client.getOutputStream());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			writer.writeObject(new String(xStream.toXML(player)));
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			client.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			writer.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}
